<?php

/**
 * get $_REQUEST value
 * @param string $key key
 * @return value , if not set return FALSE
 */
function v($key) {
    return isset($_REQUEST[$key]) ? $_REQUEST[$key] : FALSE;
}

/**
 * remove html,xml,php tag
 * @param string $str str
 * @return value
 */
function z($str) {
    return strip_tags($str);
}

/**
 * get $GLOBALS['config']'s value
 * @param string $key key
 * @return value , if not set return FALSE
 */
function C($key) {
    return isset($GLOBALS['config'][$key]) ? $GLOBALS['config'][$key] : FALSE;
}

/**
 * get $GLOBALS value
 * @param type $str string
 * @return value , if not set return FALSE
 */
function G($key) {
    return isset($GLOBALS[$key]) ? $GLOBALS[$key] : FALSE;
}

/**
 * trim
 * @param string $str string
 * @return value 
 */
function t($str) {
    return trim($str);
}

/**
 * urlencode
 * @param $str
 * @return value
 */
function u($str) {
    return urlencode($str);
}

/**
 * get $_SESSION value
 * @param type $key session_key
 * @return value , if not set return FALSE
 */
function ss($key) {
    return isset($_SESSION[$key]) ? $_SESSION[$key] : FALSE;
}

/**
 * set $_SESSION value
 * @param string $key key
 * @param type $value value
 * @return value
 */
function ss_set($key, $value) {
    return $_SESSION[$key] = $value;
}

function uses($m) {
    load('lib/' . basename($m));
}

function load($file_path) {
    $file = AROOT . $file_path;
    if (file_exists($file)) {
        //echo $file;
        require( $file );
    } else {
        //echo CROOT . $file_path;
        require( CROOT . $file_path );
    }
}

function rjoin($sp, $str, $array) {
    $ret = array();
    foreach ($array as $key => $value) {
        $ret[] = $str . trim($value, $str) . $str;
    }
    return join($sp, $ret);
}
function _debug($arr) {
    if(IS_DEBUG){print_r($arr);print("<br/>");}//chr(13).chr(10)
}
function _debugpre($arr) {
    if(IS_DEBUG){print("<pre>");print_r($arr);print("</pre>");print("<br/>");}
}
function _throwResult($retcode,$message='',$data=array()){
	header('Content-Type: text/html; charset=utf-8');
	echo decodeUnicode( json_encode( array('retcode'=>$retcode,'message'=>$message,'data'=>$data) ) );
	//echo ( json_encode( array('retcode'=>$retcode,'msg'=>$message,'data'=>$data) ) );
	exit;
}
function decodeUnicode($str) {
    return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function('$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'), $str);
}
function js_alert($msg,$return=0){
	return '<script>'.'alert("'.$msg.'");'.
		($return?'history.go(-1);':'')
		.'</script>';
}
function php_self(){
    $php_self=substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],'/')+1);
    return $php_self;
}
function php_basename($suffix='.php'){
	return (basename($_SERVER['PHP_SELF'],$suffix));
}
function getFileList($directory) {
    $files = array();
    if(is_dir($directory)) {
        if($files = scandir($directory)) {
            $files = array_slice($files,2);
        }
    }
    return $files;
}